package test;

//NEW
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

import java.util.Iterator;
import java.util.List;

import movgen.Move2;
import movgen.Util;

import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;


import search.Search2;
import state.GameState;

public class SearchTest2 {

	int depthLevel=7;
	long total=0;
	static Search2 search;
	
	@BeforeClass
	public static void setUp() throws Exception {
		//search = new Search2();
		Search2.setUsingTime(false);
	}
	
	private String execute(GameState b) {
		 long start = System.currentTimeMillis();
		 Search2.setDebug(true);
		 String moveStr = Search2.calcBestMove(b, depthLevel);
		 long end = System.currentTimeMillis();
		 long duration = end-start;
		 total=total+duration;
		 System.out.println("Time elapsed " + duration + " milliSeconds");
		 System.out.println("New Move " + moveStr);
		 return moveStr;
	}
	
	@Ignore
	public void castleOutOfCheck() {
		System.out.println("Castle out of check");
		String s = "r3k2r/1pN1bppp/p1np2q1/4p3/8/1B6/PPPQ1PPP/2KRR3 b kq - 3 16";
		GameState gs = new GameState(s);
		String reply = execute(gs);
		assertNotEquals("e8c8", reply);
	}
	// this is legal, it is not castling across check
	// even though the rook travels across the opponent's rook's attack

	private void aux(int count, String fen, String answer)
	{
		System.out.println();
		System.out.println("TEST # " + count);
		GameState gs = new GameState(fen);
		String moveStr = execute(gs);
		assertEquals(answer,moveStr);
	}
	@Ignore
	public void avoidStalemate() {
		String s1 = "k2r4/P3K1R1/8/7P/8/6P1/8/8 w - - 0 1";
		avoidStalemateAux(s1, "e7d8"); 
		
		/*s1 = "k7/P5R1/7P/8/8/6P1/4r3/5K2 w - - 0 1";
		avoidStalemateAux(s1, "f1e2"); 
		
		s1= "K7/Q7/8/8/8/8/5p2/7k w - - 0 1";
		
		avoidStalemateAux(s1, "a7f2"); 
		
		s1="8/1p4p1/2k2R1p/8/8/8/1r5r/K7 b - - 0 1";
		avoidStalemateAux(s1, "g7f6"); */
	}
	private void avoidStalemateAux(String s, String badMove) 
	{
		
		System.out.println();
		
		GameState gs = new GameState(s);
		execPrint(s);
		String moveStr = execute(gs);
		System.out.println(moveStr);
		assertNotEquals(moveStr,badMove);
	}
	private void printMoveList(List<Integer>list) {
		System.out.println("PRINT MOVELIST");
		Iterator<Integer> iter = list.iterator();
		while (iter.hasNext()) {
			int move = iter.next();
			System.out.println(Move2.toString(move));
			
		}
		System.out.println("DONE PRINT MOVELIST");
	}
	private int execPrint( String pos) {
		GameState gs = new GameState(pos);
	   
		String s = execute(gs);
		
		List<Integer> list = Util.getLegalMoveList(gs);
		printMoveList(list);
		//System.out.println("Count = " + list.size());
		return list.size();
	}
	@Test
	
	public void tester() {
		
		String fen="r1b2rk1/2q2ppp/ppn1p3/2npP3/Pb3B2/1BN2N2/1PP2PPP/R2QR1K1 w - - 4 16";
		
		GameState gs = new GameState(fen);
		String moveStr = execute(gs);
		System.out.println(moveStr);
		
	}
	/*@Test
	public void passedPawnPositions() {
		String[] fen = {
				
				"8/5pk1/4p3/7Q/8/3q4/KP6/8 b - - bm", 
				
				"r1b5/p2k1r1p/3P2pP/1ppR4/2P2p2/2P5/P1B4P/4R1K1 w - - bm",  //Bxg6;
				"6r1/1p3k2/pPp4R/K1P1p1p1/1P2Pp1p/5P1P/6P1/8 w - - bm",  //Rxc6;
				"1k2b3/4bpp1/p2pp1P1/1p3P2/2q1P3/4B3/PPPQN2r/1K1R4 w - - bm"  //f6;
		};
		
		String[] answers = {
				 "d3d5"
		};
	}*/
	
	@Test
	public void bratkoKopec() {
		// and larry kaufman
		String[] fen = {
				"1k1r4/pp1b1R2/3q2pp/4p3/2B5/4Q3/PPP2B2/2K5 b - - bm",  //BK 01
				"r1b2rk1/2q1b1pp/p2ppn2/1p6/3QP3/1BN1B3/PPP3PP/R4RK1 w - - bm",  // BK 05
				"1nk1r1r1/pp2n1pp/4p3/q2pPp1N/b1pP1P2/B1P2R2/2P1B1PP/R2Q2K1 w - - bm" , //BK07
				
		};
		
		String[] answers = {
				"d6d1",
				"c3d5",
				"h5f6",
				
		};
		
		for (int i=0; i < fen.length; i++)
			aux(i, fen[i], answers[i]);
		
		System.out.println("Total " + total);
	}
	@Test
	
		public void WinAtChess() 
		{
		
		String[] fen = {
				//"r1b1kb1r/pp2p1pp/3q4/5p2/3nQ3/P5NP/1PP2PP1/R1BBK2R w KQkq f6 0 16",
				//"r1bqkbnr/pppp1ppp/8/3Pn3/5P2/8/PPP3PP/RNBQKBNR b KQkq f3 0 5",
				"2rr3k/pp3pp1/1nnqbN1p/3pN3/2pP4/2P3Q1/PPB4P/R4RK1 w - - bm",
				"rnbqkbnr/pppp1ppp/4p3/8/6P1/5P2/PPPPP2P/RNBQKBNR b KQkq - 0 2",
				"rnbqkbnr/ppppp2p/8/5pp1/4P3/2N5/PPPP1PPP/R1BQKBNR w KQkq g6 0 3",
				"r4kr1/1b2R1n1/pq4p1/4Q3/1p4P1/5P2/PPP4P/1K2R3 w - - 0 1",
				"r3rk2/1p1n1p1Q/p1b1p3/2q3pN/8/2PB4/P1P2PP1/2KRR3 w - - 0 1",
				"8/8/8/8/8/7K/5k2/4R3 b - - 0 68",
				"5k2/6pp/p1qN4/1p1p4/3P4/2PKP2Q/PP3r2/3R4 b - - bm",   //wac005
				"2br2k1/2q3rn/p2NppQ1/2p1P3/Pp5R/4P3/1P3PPP/3R2K1 w - - bm", //wac010
				"r4q1k/p2bR1rp/2p2Q1N/5p2/5p2/2P5/PP3PPP/R5K1 w - - bm",
				"3qrbk1/ppp1r2n/3pP2p/3P4/2P4P/1P3Q2/PB6/R4R1K w - - bm",
				"6r1/3Pn1qk/p1p1P1rp/2Q2p2/2P5/1P4P1/P3R2P/5RK1 b - - bm"
				//"3q1rk1/p4pp1/2pb3p/3p4/6Pr/1PNQ4/P1PB1PP1/4RRK1 b - - bm"
		};
		String[] answers = {
				//"e4d3",
				//"e5g6",
				"g3g6",
				"d8h4",
				"d1h5",
				"e7f7",
				"d3g6",
				"f2e1",
				"c6c4",
				"h4h7",
				"e7f7",
				"f3f7",
				"g6g3"
				//"d6h2"
		};
		
		for (int i=0; i < fen.length; i++)
			aux(i, fen[i], answers[i]);
		
		System.out.println("Total " + total);
	}
	
}
